package com.mars.weixin.web.boss.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.mars.base.common.exception.AppException;
import com.mars.base.common.utils.file.cloud.UploadCloudStroe;
import com.mars.base.common.utils.web.ResponseResult;
import com.mars.base.web.BaseController;
import com.mars.base.web.model.DataGrid;
import com.mars.boss.weixin.entity.WxArticle;
import com.mars.boss.weixin.service.WxArticleService;

@RestController
@RequestMapping("/weixin/article")
public class WxArticleController extends BaseController {
	
	@Autowired
	private WxArticleService service;
	
	@Autowired
	private UploadCloudStroe uploadCloudStroe ;

	@PostMapping(value = "/add")
	public Object add(WxArticle entity) {
		try {
			this.service.insert(entity);
		} catch (AppException e) {
			return ResponseResult.addFailed();
		}
		return ResponseResult.addOk();
	}

	@GetMapping(value = "/delete/{ids}")
	public Object delete(@PathVariable String ids) {
		try {
			this.service.deleteByIds(ids);
		} catch (AppException e) {
			return ResponseResult.deleteFailed();
		}
		return ResponseResult.deleteOk();
	}

	@PostMapping(value = "/update")
	public Object update(WxArticle entity) {
		try {
			this.service.update(entity);
		} catch (AppException e) {
			return ResponseResult.updateFailed();
		}
		return ResponseResult.updateOk();
	}

	@GetMapping(value = "/get/{id}")
	public WxArticle get(@PathVariable Long id) {
		return this.service.selectById(id);
	}
	
	@GetMapping(value = "/list")
	public Object list(WxArticle entity) {
		try {
			PageInfo<WxArticle> selectPage = this.service.selectPage(getPageParam(), entity);
			DataGrid<WxArticle> dg = new DataGrid<WxArticle>() ;
			dg.setRows(selectPage.getList());
			dg.setTotal(selectPage.getTotal());
			return dg ;
		} catch (AppException e) {
			return ResponseResult.loadFailed();
		}
	}

	@GetMapping(value = "/combobox")
	public Object combobox(WxArticle entity) {
		return this.service.combobox() ;
	}
}
